<template>
	<om-formsub-outer :index="index" :checkedit="checkedit" :deleteBtn="!formdata.upperFlag">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		getEditorTitle,
		changeEditor,
		configSelectData,
		hasValue
	} from '@/static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				ratioIdKey: 'obj-year-tinstAnnualTargetPlan-title_index-ratioId-cache',
				orgCacheKey: 'obj-year-tinstAnnualTargetPlan-title_index-inst-cache',
				dutyOffCache: 'obj-year-tinstAnnualTargetPlan-title_index-dutyOff-cache',
				indexcache: "obj-year-tinstAnnualTargetPlan-title_index-index-cache",
				index1cache: "obj-year-tinstAnnualTargetPlan-title_index-index1-cache",
				firstOrder: 100,
				targetList: [],
				eTitle:""
			})
		},
		methods: {
			clearIndexId(){
				let _that = this
				_that.$refs.autoform.clear("title_index","indexId")
				_that.$refs.autoform.clear("title_index","rangeId")
				_that.$refs.autoform.clear("title_index","targetValueCode")
				_that.$refs.autoform.clear("title_index","challengeTargetCode")
				_that.$refs.autoform.clear("title_index","ratioId")
				_that.$refs.autoform.clear("title_index","indexOriginId")
				_that.$refs.autoform.clear("title_index","checksId")
				_that.$refs.autoform.clear("title_index","weight")
				_that.$refs.autoform.clear("title_index","remark")
				_that.$refs.autoform.clear("title_index","indexDefine")
				_that.$refs.autoform.clear("title_index","targetCode")
			},
			inputTargetValue(val){
				let _that = this
				_that.$refs.autoform.formdata.targetValueName = _that.$refs.autoform.formdata.logicRelationshipName + val + _that.$refs.autoform.formdata.indexUnitName
				_that.$refs.autoform.formdata.targetValueName_ = _that.$refs.autoform.formdata.logicRelationshipName + val + _that.$refs.autoform.formdata.indexUnitName
			},
			inputWeight(val){
				let _that = this
				if(Number(val) > 100){
					_that.$refs.autoform.formdata.weight = 100
				}else if(Number(val) < 1){
					_that.$refs.autoform.formdata.weight = 1
				}
			},
			getIndexOriginList(id){
				let _that = this
				_that.om.request(
					"tinstAnnualTargetPlan/getIndexOriginList",{
						instId: id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_index", "indexOriginId", "type", "selectbox")
					_that.$refs.autoform.changeEditor("title_index", "indexOriginId", "data", configSelectData(res,"id", "dicname"))
					_that.$refs.autoform.initSelectIndex("title_index", "indexOriginId")
				})
			},
			changeType(logicRelationshipName,indexUnitName){
				let _that = this
				_that.$refs.autoform.formdata.logicRelationshipName = logicRelationshipName
				_that.$refs.autoform.formdata.indexUnitName =  indexUnitName
				_that.$refs.autoform.changeEditor("title_index", "targetValueCode","required",true)
				_that.$refs.autoform.changeEditor("title_index", "targetValueCode", "type","numberbox")
				_that.$refs.autoform.changeEditor("title_index", "targetValueCode", "prefix", logicRelationshipName)
				_that.$refs.autoform.changeEditor("title_index", "targetValueCode", "suffix", indexUnitName)
				_that.$refs.autoform.changeEditor("title_index", "targetValueCode", "onSelect", 'inputTargetValue')
				
				_that.$refs.autoform.changeEditor("title_index", "challengeTargetCode", "type","numberbox")
				_that.$refs.autoform.changeEditor("title_index", "challengeTargetCode", "suffix", indexUnitName)
				_that.$refs.autoform.changeEditor("title_index", "challengeTargetCode","prompt","请输入")
				_that.$refs.autoform.changeEditor("title_index", "remark1", "type", "textarea")
			},
			checkIndexAttrId() {
				let _that = this
				uni.setStorage({
					key: _that.index1cache,
					data:_that.targetList,
					success: (res) => {
						_that.$refs.autoform.changeEditor("title_index", "targetCode", "type", "linkbox")
						_that.$refs.autoform.changeEditor("title_index", "targetCode", {
							link: "../../public/search/search",
							"linkParam": {
								other: JSON.stringify({}),
								cachename: _that.indexcache,
								otherCache: _that.index1cache,
								defaultData: true,
								initload: true,
								namefield: "orderNum",
								extrafield: 'targetRemark',
								placeholder: "请输入"+_that.eTitle
							}
						})
					}
				})
			},
			getIndexDefine(id) {
				let _that = this
				_that.om.request(
					"indexDict/getIndexDefine", {
						indexId: id
					}
				).then(res => {
					_that.$refs.autoform.formdata.indexDefine = res.value
					// _that.$refs.autoform.changeEditor("title_index", "indexDefine", "type", "lookfilebox")
					// _that.$refs.autoform.changeEditor("title_index", "indexDefine", "isText", true)
					// _that.$refs.autoform.changeEditor("title_index", "indexDefine", "remark", res.value || '')
					// _that.$refs.autoform.changeEditor("title_index", "indexDefine", "disabled", true)
					// _that.$refs.autoform.changeEditor("title_index", "indexDefine", "showClear", false)
				})
			},
			selectChecksId(data){
				let _that = this
				_that.$refs.autoform.changeEditor("title_index", "weight","required",true)
				_that.$refs.autoform.changeEditor("title_index", "weight", "type", "numberbox")
			},
			clearChecksId(){
				let _that = this
				_that.$refs.autoform.clear("title_index","checksId")
				_that.$refs.autoform.clear("title_index","weight")
				_that.$refs.autoform.changeEditor("title_index", "weight","required",false)
			},
			getCheckList() {
				let _that = this
				_that.om.request(
					"torgAnnualTargetPlan/getCheckList",
				).then(res => {
					_that.$refs.autoform.changeEditor("title_index", "checksId", "type", "selectbox")
					_that.$refs.autoform.changeEditor("title_index", "checksId", "data", configSelectData(res,"id", "text"))
					_that.$refs.autoform.initSelectIndex("title_index","checksId")
				})
			},
			getIndexUserList(id) {
				let _that = this
				_that.$refs.autoform.changeEditor("title_index", "dutyOffId", {
					link: "../../public/search/search",
					"linkParam": {
						other: JSON.stringify({
							instId: _that.instId,
							indexId: id
						}),
						url: "torgMediumTermPlan/getIndexUserList",
						namefield: "userPosName",
						extrafield: 'instName',
						initload: true,
						cachename: _that.dutyOffCache
					}
				})
			},
			getCategoryList(id) {
				let _that = this
				_that.om.request(
					"indexCategory/findIndexCategoryList", {
						indexDictId: id
					}
				).then(res => {
					_that.$refs.autoform.changeEditor("title_index", 'rangeId', 'type', 'selectbox')
					_that.$refs.autoform.changeEditor("title_index", 'rangeId', "data", configSelectData(res,
						"indexCategoryId", "indexCategoryName"))
						_that.$refs.autoform.initSelectIndex("title_index", 'rangeId')
				})
			},
			getRatio() {
				let _that = this
				_that.$refs.autoform.changeEditor("title_index", "ratioId", "type", "linkbox")
				_that.$refs.autoform.changeEditor("title_index", "ratioId", {
					link: "../../public/search/search",
					"linkParam": {
						other: JSON.stringify({
							
						}),
						url: "dataDict/getCombobox.do?code=DD021001",
						namefield: "dicname",
						extrafield: "initNum",
						initload: true,
						cachename: _that.ratioIdKey,
						placeholder: "请输入"
					}
				})
			}
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
						key: _that.orgCacheKey,
						success: (res) => {
							if (res.data) {
								let d = res.data
								let formdata = _that.$refs.autoform.formdata
								formdata.indexId = d.id
								formdata.indexName = d.dicname
								formdata.indexAttrId = d.indexAttrId
								formdata.indexAttrName = d.indexAttrName
								formdata.inputPrecision = d.inputPrecision
								formdata.weight = d.power
								_that.checkIndexAttrId()
								_that.getCategoryList(d.id)
								_that.getIndexUserList(d.id)
								_that.getCheckList()
								_that.getRatio()
								_that.getIndexDefine(d.id)
								_that.changeType(d.logicRelationshipName,d.indexUnitName)
								_that.getIndexOriginList(d.id)
								uni.removeStorage({
									key: _that.orgCacheKey
								})
							}
						}
					}),
					uni.getStorage({
						key: _that.dutyOffCache,
						success: (res) => {
							if (res.data) {
								let d = res.data
								let formdata = _that.$refs.autoform.formdata
								formdata.dutyOffId = d.id
								formdata.dutyOffName = d.userPosName
								formdata.dutyInstName = d.instName
								uni.removeStorage({
									key: _that.dutyOffCache
								})
							}
						}
					})
				uni.getStorage({
					key: _that.indexcache,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.targetCode = d.orderNum
							uni.removeStorage({
								key: _that.indexcache
							})
						}
					}
				});
				uni.getStorage({
					key: _that.ratioIdKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.ratioId = d.id
							formdata.ratioName = d.dicname
							uni.removeStorage({
								key: _that.ratioIdKey
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					_that.instId = e.instId
					_that.targetList = JSON.parse(e.targetList)
					_that.eTitle = e.title
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							if (res.data && Object.prototype.toString.call(res.data) ===
								'[object Array]') {
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								let title = getEditorTitle("title_index", "indexId", _that.autoformdata)
								let objFormCode = JSON.parse(uni.getStorageSync('objFormCode')) 
								changeEditor("title_index","orderNum",{
									defaultName:objFormCode.DICT_CODE_R+(_that.order),
									defaultValue:objFormCode.DICT_CODE_R+(_that.order)
								},null,_that.autoformdata)
								if(_that.formdata.upperFlag){
									changeEditor("title_index", "indexId",'type','labelbox',_that.autoformdata)
								}else{
									changeEditor("title_index", "indexId", {
										link: "../../public/search/search",
										"linkParam": {
											other: JSON.stringify({
												instId: _that.instId
											}),
											url: "indexDict/getAllIndex2",
											namefield: "dicname",
											extrafield: "indexUnitName",
											notefield: "logicRelationshipName",
											initload: true,
											cachename: _that.orgCacheKey,
											placeholder: "请输入" + title
										}
									}, null, _that.autoformdata)
								}
							}
						}
					})
				}
				_that.$nextTick(function() {
					if (_that.$refs.autoform.formdata.indexId) {
						_that.getCategoryList(_that.$refs.autoform.formdata.indexId)
						_that.getIndexUserList(_that.$refs.autoform.formdata.indexId)
						_that.getCheckList()
						_that.getRatio()
						_that.getIndexDefine(_that.$refs.autoform.formdata.indexId)
						_that.changeType(_that.$refs.autoform.formdata.logicRelationshipName,_that.$refs.autoform.formdata.indexUnitName)
						_that.getIndexOriginList(_that.$refs.autoform.formdata.indexId)
						_that.checkIndexAttrId()
						if(_that.$refs.autoform.formdata.checksId){
							_that.$refs.autoform.changeEditor("title_index", "weight","required",true)
							_that.$refs.autoform.changeEditor("title_index", "weight", "type", "numberbox")
						}
					}
				})
			})
		}
	}
</script>

<style>
</style>
